home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************************
- DotsModule.c
-
-
- DOCUMENTATION
-
- an example module for ScreenLight using PPC-style globals
-
-
- MODULE CODE RESOURCES
-
- Here are the types we define for the kinds of Modules one
- might produce.
-
- App: creator='SS-ß'
-
- 68k Only: type='SS-m'
- code='SS-m' 0
-
- PPC Only: type='SS-p',
-
- Fat Binary: type='SS-f',
- code='SS-m' 0
-
-
- HISTORY
-
-
-
- Copyright )WORK-IN_PROGRESS noEsis Software Construction
- **********************************************************************************/
-
-
- //*********************************************************************************
- // I N C L U D E I N C L U D E S
-
- #include <A4Stuff.h>
- #include "ModuleRoutines.h"
-
-
- //*********************************************************************************
- // F O R W A R D G L O B A L S
-
-
- OSErr AboutDefault( ModuleParamBlkPtr mpPtr);
- void GetNewPt( ModuleParamBlkPtr mpPtr);
-
-
- //*********************************************************************************
- // G L O B A L S C O N S T A N T S
-
- #define kPtCnt 16
- #define dMinDrawTime 5
-
- long gLastDraw;
- long gCurHue;
- long gCurBrit;
- long gBritDir; // 1,0
- Point gCurDir; // h,v (-1,0,1)
- Point gPts[ kPtCnt];
- Pattern gWhitePat, gBlackPat;
-
-
- //*********************************************************************************
- // main - MPW Linker wanted a routine named main.
- //*********************************************************************************
- OSErr main( ModuleParamBlkPtr mpPtr)
- {
- OSErr err;
-
- EnterCodeResource();
-
- err = ModOpen( mpPtr);
- ExitCodeResource();
- return err;
- }
-
- OSErr ModOpen( ModuleParamBlkPtr mpPtr)
- {
- RGBColor tC;
- short c;
- Rect r;
- OSErr err;
-
- EnterCodeResource();
-
- mpPtr->mProcs.drawProc = NewModDrawProc(ModDraw);
- mpPtr->mProcs.closeProc = NewModCloseProc(ModClose);
- mpPtr->mProcs.chngPrefsProc = NewModChangePrefsProc(ModChangePrefs);
- mpPtr->mProcs.aboutProc = NewModAboutProc( AboutDefault);
-
- mpPtr->mStorage = 0;
-
- gLastDraw = TickCount();
- gCurHue = IRandom( 0,360);
- gCurBrit = 70;
- gBritDir = 1;
- for ( c=0;c<8;c++)
- gWhitePat.pat[c] = 0x00;
- for ( c=0;c<8;c++)
- gBlackPat.pat[c] = 0xFF;
-
- gCurDir.h = gCurDir.v = 1;
-
- PenNormal();
- BackColor( blackColor);
- if ( GetPortPixDepth( mpPtr->mGraf) > 1) {
- CalcHSBtoRGB( gCurHue, 80, gCurBrit, &tC);
- RGBForeColor( &tC);
- } else
- ForeColor( whiteColor);
-
- gPts[0].h = gPts[0].v = 20;
- for (c=0;c<kPtCnt-1;c++) {
- r.left = gPts[0].h;
- r.top = gPts[0].v;
- r.right = r.left + 4;
- r.bottom = r.top + 4;
- if ( IRandom(0,2)==1)
- FillOval( &r, (Pattern*)gWhitePat.pat);
- else
- FillOval( &r, (Pattern*)gBlackPat.pat);
- GetNewPt( mpPtr);
- }
- r.left = gPts[0].h;
- r.top = gPts[0].v;
- r.right = r.left + 4;
- r.bottom = r.top + 4;
- if ( IRandom(0,6)==1)
- FillOval( &r, (Pattern*)gWhitePat.pat);
- else
- FillOval( &r, (Pattern*)gBlackPat.pat);
-
- ExitCodeResource();
- return noErr;
- }
-
- // Shift the existing points back, add a new Point at index 0
- // 01234567 -> n0123456
- void GetNewPt( ModuleParamBlkPtr mpPtr)
- {
- Boolean bounce;
- short c, extra, extDir;
-
- BlockMoveData( &gPts[0], &gPts[1], sizeof(Point)*(kPtCnt-1));
-
- // check if we need to change direction
- bounce = false;
- if ( gPts[0].h > mpPtr->mGraf->portRect.right) {
- gCurDir.h = ( IRandom(0,2)==1)?(0):(-1);
- if ( gCurDir.v == 0) {
- c = IRandom( 0,3); gCurDir.v = c - 1;
- }
- bounce = true;
- } else if ( gPts[0].h < mpPtr->mGraf->portRect.left) {
- gCurDir.h = ( IRandom(0,2)==1)?(0):(1);
- if ( gCurDir.v == 0) {
- c = IRandom( 0,3); gCurDir.v = c - 1;
- }
- bounce = true;
- }
-
- if ( gPts[0].v > mpPtr->mGraf->portRect.bottom) {
- gCurDir.v = ( IRandom(0,2)==1)?(0):(-1);
- if ( gCurDir.h == 0) {
- c = IRandom( 0,3); gCurDir.h = c - 1;
- }
- bounce = true;
- } else if ( gPts[0].v < mpPtr->mGraf->portRect.top) {
- gCurDir.v = ( IRandom(0,2)==1)?(0):(1);
- c = IRandom( 0,3); gCurDir.h = c - 1;
- if ( gCurDir.h == 0) {
- c = IRandom( 0,3); gCurDir.h = c - 1;
- }
- bounce = true;
- }
-
- if ( !bounce && IRandom(0,108*6)==1) {
- do {
- c = IRandom( 0,3); gCurDir.h = c - 1;
- c = IRandom( 0,3); gCurDir.v = c - 1;
- } while ( (gCurDir.h == 0 && gCurDir.v == 0));
- }
-
- // Now make new point
- if ( !bounce && IRandom(0,2)==1) {
- extra = true;
- extDir = IRandom(0,2);
- } else extra = false;
-
- if ( gCurDir.h == 1) {
- gPts[0].h = gPts[1].h + 4;
- if ( extra) {
- if ( extDir == 1) gPts[0].v += IRandom( 1,6);
- else gPts[0].v -= IRandom( 1,6);}
- } else if ( gCurDir.h == -1) {
- gPts[0].h = gPts[1].h - 4;
- if ( extra) {
- if ( extDir == 1) gPts[0].v -= IRandom( 1,6);
- else gPts[0].v += IRandom( 1,6);}
- }
-
- if ( gCurDir.v == 1) {
- gPts[0].v = gPts[1].v + 4;
- if ( extra) {
- if ( extDir == 1) gPts[0].h += IRandom( 1,6);
- else gPts[0].h -= IRandom( 1,6);}
- } else if ( gCurDir.v == -1) {
- gPts[0].v = gPts[1].v - 4;
- if ( extra) {
- if ( extDir == 1) gPts[0].h -= IRandom( 1,6);
- else gPts[0].h += IRandom( 1,6);}
- }
-
-
- return;
- }
-
- //*************************************************
- OSErr ModDraw( ModuleParamBlkPtr mpPtr)
- {
- RGBColor tC;
- Rect r;
- long t;
-
- EnterCodeResource();
-
- if ( gLastDraw + dMinDrawTime > TickCount() )
- goto done;;
-
- if ( IRandom( 1,6) == 1) {
- gLastDraw ++;
- goto done;
- }
- if ( IRandom( 1,6) == 1) {
- gLastDraw --;
- goto done;
- }
-
- PenNormal();
- BackColor( blackColor);
- r.left = gPts[kPtCnt-1].h;
- r.top = gPts[kPtCnt-1].v;
- r.right = r.left + 4;
- r.bottom = r.top + 4;
- FillOval( &r, (Pattern*)gBlackPat.pat);
-
- GetNewPt( mpPtr);
-
- if ( GetPortPixDepth( mpPtr->mGraf) > 1) {
- CalcHSBtoRGB( gCurHue, 88, gCurBrit, &tC);
- if ( IRandom(0,4) == 1) gCurHue++;
- if ( IRandom(0,2) == 1) {
- if ( gCurBrit == 100 ) {
- if ( IRandom(0,108*4) == 1) {
- if ( gBritDir == 1) gCurBrit++;
- else gCurBrit--;
- }
- } else {
- if ( IRandom(0,3) == 1) {
- if ( gBritDir == 1) gCurBrit++;
- else gCurBrit--;
- }
- }
- }
- RGBForeColor( &tC);
- } else
- ForeColor( whiteColor);
- if ( gCurHue > 360) gCurHue = 0;
- if ( gCurBrit > 100) gBritDir = 0;
- else if ( gCurBrit == 12) gBritDir = 1;
-
- r.left = gPts[0].h;
- r.top = gPts[0].v;
- r.right = r.left + 4;
- r.bottom = r.top + 4;
- if ( IRandom(0,6)==1)
- FillOval( &r, (Pattern*)gWhitePat.pat);
- else
- FillOval( &r, (Pattern*)gBlackPat.pat);
-
- gLastDraw = TickCount();
-
- done:
- ExitCodeResource();
- return noErr;
- }
-
- //*************************************************
- OSErr ModClose( ModuleParamBlkPtr mpPtr)
- {
- return noErr;
- }
-
-
- //*************************************************
- OSErr ModChangePrefs( ModuleParamBlkPtr mpPtr)
- {
- return noErr;
- }
-
-
- //*************************************************
- // Sample About Procedure for a Module
-
- OSErr AboutDefault( ModuleParamBlkPtr mpPtr)
- {
- Rect tmpR;
- EventRecord evt;
- PicHandle picH;
- Rect dR;
- short tWid, tHt, pWid, pHt;
- short offH, offV;
-
- EnterCodeResource();
-
- // GrafPort is set on entry
- tmpR = mpPtr->mGraf->portRect;
-
- // let the ForeColor be as it may
- // just for fun..
- PaintRect( &tmpR);
-
- picH = GetPicture( 128);
- dR = (**picH).picFrame;
- tWid = tmpR.right - tmpR.left;
- tHt = tmpR.bottom - tmpR.top;
- pWid = dR.right - dR.left;
- pHt = dR.bottom - dR.top;
-
- offH = (tWid - pWid)/2;
- offV = (tHt - pHt)/2;
- OffsetRect(&dR,-dR.left,-dR.top);
- OffsetRect(&dR, offH, offV);
-
- DrawPicture( picH, &dR);
-
- while ( !WaitNextEvent( mDownMask+keyDownMask, &evt, GetCaretTime(), 0))
- ;
-
- ForeColor( blackColor);
- PaintRect( &tmpR);
-
- ExitCodeResource();
- return;
- }
-
-
- //**********************************************************************************
- // E N D O F L I S T I N G
-